<template>
  <view class="container">

    <!-- 扫描输入区域 -->
    <view class="scan-section">
      <view class="scan-header">
        <up-text :text="'扫描'+label" class="scan-label"></up-text>
        <up-input v-model="scanInput" :placeholder="'请扫描'+label" @confirm="handleScan" :focus="true"
                  class="scan-input"/>
      </view>

      <view class="status-message">
        <up-text :text="state.text" :type="statusType"></up-text>
      </view>
    </view>

    <!-- 物料信息卡片 -->
    <view class="info-section">
      <up-divider text="当前物料信息" lineColor="#eee" textColor="#666"></up-divider>
      <view class="info-card">
        <view class="info-row">
          <view class="info-item">
            <text class="info-label">成品名称:</text>
            <text class="info-value">{{ state.planInfo.materialName || '-' }}</text>
          </view>
          <view class="info-item">
            <text class="info-label">BOM版本:</text>
            <text class="info-value">{{ state.planInfo.positionVer || '-' }}</text>
          </view>
        </view>

        <view class="info-row">
          <view class="info-item">
            <text class="info-label">站位编号:</text>
            <text class="info-value">{{ state.positionInfo.systemRealPosition || '-' }}</text>
          </view>
          <view class="info-item">
            <text class="info-label">物料编号:</text>
            <text class="info-value">{{ state.positionInfo.materialCode || '-' }}</text>
          </view>
        </view>

        <view class="info-row">
          <view class="info-item">
            <text class="info-label">物料名称:</text>
            <text class="info-value">{{ state.positionInfo.materialName || '-' }}</text>
          </view>
          <view class="info-item">
            <text class="info-label">物料规格:</text>
            <text class="info-value">{{ state.positionInfo.materialDesc || '-' }}</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 未扫站位表格 -->
    <view class="table-section">
      <up-divider text="未扫站位信息" lineColor="#eee" textColor="#666"></up-divider>
      <up-table2 @touchmove.stop :data="state.tableData" :columns="state.columns" stripe border class="data-table"/>
    </view>
  </view>
</template>

<script setup>
import {computed, reactive, ref} from "vue";
import {getActivatePlan} from "@api/product/order/workOrderPlan.js";
import {checkMaterial, checkPosition, noCheckList} from "@api/product/order/planPosition";
import {onPullDownRefresh, onReachBottom} from '@dcloudio/uni-app'

const scanInput = ref("");
const label = ref('站位')

const state = reactive({
  from: {
    lineId: '',
    planId: '',
    positionId: '',
    materialBarcode: '',
    nextFlag: false,
  },
  planInfo: {},
  positionInfo: {},
  text: '请扫描站位条码',
  tableData: [],
  pages: 0,
  pageNum: 1,
  columns: [{
    title: '站位',
    key: 'systemRealPosition',
    width: '60px',
  },
    {
      title: '机器',
      key: 'machineName',
      width: '60px'
    },
    {
      title: '物料编码',
      key: 'materialCode',
      width: '120px'
    },
    {
      title: '物料名称',
      key: 'materialName',
      width: '150px'
    },
    {
      title: '规格描述',
      key: 'materialDesc',
      width: '500px'
    }
  ]
})

// 根据状态计算消息类型
const statusType = computed(() => {
  if (state.text.includes('成功')) return 'success'
  if (state.text.includes('失败') || state.text.includes('错误')) return 'error'
  return 'primary'
})

function handleScan() {
  if (label.value === "站位") {
    state.from.positionId = scanInput.value;
    checkPosition(state.from).then(res => {
      state.positionInfo = res.data;
      label.value = "物料";
      scanInput.value = "";
      state.text = "请扫描物料条码";
    }).catch((e) => {
      state.text = "站位扫描失败，" + e.message;
    })
  } else if (label.value === "物料") {
    state.from.materialBarcode = scanInput.value;
    checkMaterial(state.from).then(res => {
      state.text = "站位【" + state.positionInfo.systemRealPosition + "】检查成功";
      // 这里可以添加成功后更新表格数据的逻辑
      label.value = "站位";
      scanInput.value = "";
      loadNoCheckData()
    }).catch((e) => {
      state.text = "物料扫描失败，" + e.message;
    })


  }
}

function loadPlanInfo() {
  getActivatePlan(state.from.lineId).then(res => {
    state.planInfo = res.data;
    state.from.planId = res.data.planId;
    loadNoCheckData();
  })
}

function loadNoCheckData() {
  let params = {
    planId: state.from.planId,
    ipqc: false,
    pageNum: state.pageNum,
    pageSize: 10,
  }
  noCheckList(params).then(res => {
    state.tableData.push(...res.data.list);
    state.pages = res.data.pages;
  })
}

onShow(() => {
  state.from.lineId = uni.getStorageSync('selectLine')
  loadPlanInfo();
})

onPullDownRefresh(async () => {
  state.from.lineId = uni.getStorageSync('selectLine')
  scanInput.value = '';
  label.value = '站位';
  state.text = '请扫描站位条码';
  state.pageNum = 1;
  state.positionInfo = {};
  state.tableData = [];
  loadNoCheckData();
  uni.stopPullDownRefresh()
})

onReachBottom(() => {
  if (state.pageNum >= state.pages) {
    return;
  }
  state.pageNum++;
  loadNoCheckData();
})

</script>

<style scoped>
.container {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background-color: #f8f8f8;
  min-height: 100vh;
}

/* 扫描区域样式 */
.scan-section {
  background: #fff;
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.scan-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.scan-label {
  width: 80px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
}

.scan-input {
  flex: 1;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
}

.status-message {
  padding: 6px 0;
  font-size: 13px;
}

/* 信息卡片样式 */
.info-section {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.info-card {
  padding: 10px 12px;
}

.info-row {
  display: flex;
  margin-bottom: 8px;
}

.info-item {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.info-label {
  font-size: 12px;
  color: #666;
  margin-bottom: 2px;
}

.info-value {
  font-size: 13px;
  color: #333;
  font-weight: 500;
}

/* 表格区域样式 */
.table-section {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.data-table {
  font-size: 12px;
}
</style>